package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.BgAlarmHistoryLog;
import com.example.service.IBgAlarmHistoryLogService;
import com.example.service.IBgDeviceHistoryLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ysr_xian
 * @since 2022-05-29
 */
@RestController
@RequestMapping("/bgAlarmHistoryLog")
public class BgAlarmHistoryLogController {

    @Autowired
    private IBgAlarmHistoryLogService bgAlarmHistoryLogService;

    @Autowired
    private IBgDeviceHistoryLogService bgDeviceHistoryLogService;

    @GetMapping("/list")
    public IPage<BgAlarmHistoryLog> list(@RequestParam(defaultValue = "device1") String deviceName,
                                         @RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize) {
        QueryWrapper<BgAlarmHistoryLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_name", deviceName); // 条件：名字相等
        queryWrapper.orderByDesc("gmt_create");  // 按日期升序
        return bgAlarmHistoryLogService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @PostMapping("delete")
    public boolean delete(@RequestBody BgAlarmHistoryLog bgAlarmHistoryLog) {
        bgDeviceHistoryLogService.removeById(bgAlarmHistoryLog.getBgDeviceId());
        return bgAlarmHistoryLogService.removeById(bgAlarmHistoryLog.getId());
    }

}

